<?php

/*
 * DB.inc
 */
$MYSQL_ERRNO = '';
$MYSQL_ERROR = '';

function db_connect($dbname='') {
   global $dbhost, $dbusername, $dbuserpassword, $default_dbname;
   global $MYSQL_ERRNO, $MYSQL_ERROR;

   $link_id = mysql_connect($dbhost, $dbusername, $dbuserpassword);
   if(!$link_id) {
      $MYSQL_ERRNO = 0;
      $MYSQL_ERROR = "Falha na conexão com o host : $dbhost.";
      return 0;
   }
   else if(empty($dbname) && !mysql_select_db($default_dbname)) {
      $MYSQL_ERRNO = mysql_errno();
      $MYSQL_ERROR = mysql_error();
      return 0;
   }
   else if(!empty($dbname) && !mysql_select_db($dbname)) {
      $MYSQL_ERRNO = mysql_errno();
      $MYSQL_ERROR = mysql_error();
      return 0;
   }
   else return $link_id;
}

function sql_error() {
   global $MYSQL_ERRNO, $MYSQL_ERROR;

   if(empty($MYSQL_ERROR)) {
      $MYSQL_ERRNO = mysql_errno();
      $MYSQL_ERROR = mysql_error();
   }
   return "$MYSQL_ERRNO: $MYSQL_ERROR";
}

function list_album()
{
    global $default_dbname;
    $link_id = db_connect($default_dbname);
    if(!$link_id) error_message (sql_error ());
    $query = "SELECT AlbumID,nome,UID,descricao,dtcadastro from TBAlbum order by dtcadastro;";
    $result = mysql_query($query);
    if(!$result) error_message (sql_error());
    while ($query_data = mysql_fetch_array($result))
        {
            
            $AlbumID =      $query_data['AlbumID'];
            $nome =         $query_data['nome'];
            $UID =          $query_data['UID'];
            $descricao =    $query_data['descricao'];
            $dtcadastro =   $query_data['dtcadastro'];
            $params = base64_encode($AlbumID . "|" . $nome ."|". $UID ."|". $descricao ."|". $dtcadastro);
            
            echo "<li><a href=\"index.php?action=detail&params=$params\">$nome</a></li>";						
        }
    
}


function list_albumDetail($albumID)
{
    global $default_dbname;
    $link_id = db_connect($default_dbname);
    if(!$link_id) error_message (sql_error ());
    $query = "SELECT AlbumID,nome,descricao from TBAlbum where AlbumID=$albumID;";
    $result = mysql_query($query);
    if(!$result) error_message (sql_error());
    while ($query_data = mysql_fetch_array($result))
        {
        $nome =         $query_data['nome'];
        $descricao =    $query_data['descricao'];
        
        echo "<p><a href=\"#\">Nome do Album: </a>$nome.</p>";
        echo "<p><a href=\"#\">Descrição: </a>$descricao.</p>";
        
        }
    
    
    
}


?>
